package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.collect.ListUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.ICommentDataDAL;
import com.qire.manhua.model.entity.ComicComment;
import com.qire.manhua.model.viewModel.impl.ComicCommentListViewModel;
import com.qire.manhua.model.vo.CommentVO;
import com.qire.manhua.model.vo.OkResponseVO;

import java.util.List;

/**
 * 漫画列表数据服务
 */
public class ComicCommentListService extends DataService {

    private final ICommentDataDAL commentDataDAL = WarehouseFactory.create(this, ICommentDataDAL.class);

    private final ComicCommentListViewModel viewModel;

    public ComicCommentListService(ComicCommentListViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /** 评论列表数据 */
    public void getCommentList() {
        commentDataDAL.fetchComicCommentList(viewModel.comicBookId(), viewModel.chapterId(), viewModel.pageNo());
    }
    @StrategyAnnotation(event = Url.comment_list)
    public void fetchComicCommentListApiCallBack(List<CommentVO> dataVOList, boolean isCache){

        viewModel.refreshLayoutFinish(300);
        if (ListUtil.isEmpty(dataVOList)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        if (viewModel.isFirstPage()) {
            viewModel.clearComicComment();
        }

        ComicComment[] comicComments = new ComicComment[dataVOList.size()];
        for (int index = 0; index < comicComments.length; index++) {
            comicComments[index] = dataVOList.get(index).toComicComment();
        }
        viewModel.loadComicComment(comicComments);


        if (!isCache) {
            viewModel.nextPage();
        }
    }

    /** 点赞给喜欢的评论*/
    public void voteToGiveLikeComment(int bookId, int commentId) {
        commentDataDAL.voteToGiveLikeComment(bookId, commentId);
    }
    @StrategyAnnotation(event = Url.comment_like)
    public void voteToGiveLikeCommentApiCallBack(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar(dataVO.ok, Prompt.SUCCESS);
    }

    /**
     * 举报评论
     * @see ICommentDataDAL#complaintComment(int, int, int, String)
     */
    public void complaintComment(int bookId, int commentId, int reportType, String content) {
        commentDataDAL.complaintComment(bookId, commentId, reportType, content);
    }
    @StrategyAnnotation(event = Url.comment_report)
    public void complaintCommentApiCallBack(OkResponseVO dataVO, boolean isCache) {
        if(dataVO.isOK()){
            ToastHelper.showSnackBar("举报成功", Prompt.SUCCESS);
        } else {
            ToastHelper.showSnackBar("举报失败", Prompt.ERROR);
        }
    }

}
